<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right"
                       style="background-color: #E4E7ED;padding: 8px;margin-bottom: 20px">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>新增用户</el-breadcrumb-item>
        </el-breadcrumb>
        <el-form :model="air" :rules="rules" ref="airForm" label-width="100px" style="width: 500px" size="small">
            <el-form-item label="请选择机型" prop="airCode"  >
                <el-input v-model="air.airCode" placeHolder="输入新增机型" style="width: 400px;float: left"/>
            </el-form-item>
            <el-form-item label="购买头等舱" prop="airCode">
                <el-select v-model="air.airFna" clearable placeholder="请选择是否购买公头等舱" style="width: 400px;float: left">
                    <el-option
                            v-for="item in airFna_options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="头等舱人数" prop="airF">
                <el-input v-model="air.airF" placeHolder="请输入头等舱人数" style="width: 400px;float: left"/>
            </el-form-item>

            <el-form-item label="购买公务舱" prop="airCode"  >
                <el-select v-model="air.airCna" clearable placeholder="请选择是否购买公务舱" style="width: 400px;float: left">
                    <el-option
                            v-for="item in airCna_options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="公务舱人数" prop="airC" >
                <el-input v-model="air.airC" placeHolder="请输入公务舱人数" style="width: 400px;float: left"/>
            </el-form-item>

            <el-form-item label="购买经济舱" prop="airCode">
                <el-select v-model="air.airYna" clearable placeholder="请选择是否购买经济舱" style="width: 400px;float: left">
                    <el-option
                            v-for="item in airYna_options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="经济舱人数" prop="airY">
                <el-input v-model="air.airY" placeHolder="请输入经济舱人数" style="width: 400px;float: left"/>
            </el-form-item>









            <el-form-item>
                <el-button type="primary" @click="registry" v-bind:disabled="regBtnSts">立即创建</el-button>
                <el-button @click="clear">清空</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>


<script>
    export default {
        name: "AddAir",
        methods: {
            registry() {
                //将提交按钮设置为不可用状态 按钮动态加disable属性
                this.regBtnSts = true;
                //1、先验证表单格式没问题了，再允许发送请求
                this.$refs['airForm'].validate((valid) => {
                    if (valid) {
                        //clone对象
                        if(this.air.airFna=="不存在"&&this.air.airYna=="不存在"&&this.air.airCna=="不存在"){
                            this.air.airTotal=0;
                            this.air.airF=0;
                            this.air.airY=0;
                            this.air.airC=0;
                        }
                        if(this.air.airFna=="不存在"&&this.air.airYna=="不存在"&&this.air.airCna=="存在"){
                            this.air.airTotal=parseInt(this.air.airC);
                            this.air.airF=0;
                            this.air.airY=0;
                        }
                        if(this.air.airYna=="不存在"&&this.air.airCna=="不存在"&&this.air.airFna=="存在"){
                            this.air.airTotal=parseInt(this.air.airF);
                            this.air.airY=0;
                            this.air.airC=0;
                        }
                        if(this.air.airCna=="不存在"&&this.air.airFna=="不存在"&&this.air.airYna=="存在"){
                            this.air.airTotal=parseInt(this.air.airY);
                            this.air.airF=0;
                            this.air.airC=0;
                        }
                        if(this.air.airCna=="不存在"&&this.air.airFna=="存在"&&this.air.airYna=="存在"){
                            this.air.airTotal=parseInt(this.air.airY)+parseInt(this.air.airF);
                            this.air.airC=0;
                        }
                        if(this.air.airYna=="不存在"&&this.air.airCna=="存在"&&this.air.airFna=="存在"){
                            this.air.airTotal=parseInt(this.air.airC)+parseInt(this.air.airF);
                            this.air.airY=0;
                        }
                        if(this.air.airFna=="不存在"&&this.air.airYna=="存在"&&this.air.airCna=="存在"){
                            this.air.airTotal=parseInt(this.air.airY)+parseInt(this.air.airC);
                            this.air.airF=0;
                        } if(this.air.airFna=="存在"&&this.air.airYna=="存在"&&this.air.airCna=="存在"){
                            this.air.airTotal=parseInt(this.air.airY)+parseInt(this.air.airC)+parseInt(this.air.airF);
                        }
                        let payLoad = this.air;
                        //删除payLoad的checkPass属性
                        delete payLoad.checkPass;
                        // 进行表单的提交
                        axios.post("/air/save", payLoad).then(response => {
                            this.regBtnSts = false;
                            if (response.data === 'success') {
                                this.$notify({
                                    title: "提示",
                                    message: "用户保存成功",
                                    type: "success"
                                });
                                //一旦提交成功，自动关闭窗口

                                //刷新表格数据
                                this.findList();
                            }
                        })
                    } else {
                        this.regBtnSts = false;
                        return false;
                    }
                })
            },


            clear() {
                this.air = {
                    airCode: "",
                    airFna: "",
                    airF: "",
                    airCna: "",
                    airC: "",
                    airYna: "",
                    airY: "",
                    airTotal: ""
                }
            }
        },
        data() {

            //通过正则验证数字
            let validF = (rules, value, callback) => {

                let pattern = /^([0-9]|[012][0-9]|30)$/;
                if (!pattern.test(value)) {
                    return callback(new Error("头等舱的容纳人数应在0-30之间，请重新输入"))
                }
                return callback();
            };
            let validC = (rules, value, callback) => {

                let pattern = /^([0-9]|[012][0-9]|30)$/;
                if (!pattern.test(value)) {
                    return callback(new Error("商务的容纳人数应在0-30之间，请重新输入"))
                }
                return callback();
            };
            let validY = (rules, value, callback) => {

                let pattern = /^([0-9]|[012][0-9]|30)$/;
                if (!pattern.test(value)) {
                    return callback(new Error("经济舱的容纳人数应在0-30之间，请重新输入"))
                }
                return callback();
            };
            return {
                regBtnSts: false,
                options: [{
                    value: '波音747',
                    label: '波音747'
                }, {
                    value: '波音743',
                    label: '波音743'
                }, {
                    value: '波音742',
                    label: '波音742'
                }, {
                    value: '波音741',
                    label: '波音741'
                }, {
                    value: '波音740',
                    label: '波音740'
                }],
                airFna_options: [{
                    value: '存在',
                    label: '购买'
                }, {
                    value: '不存在',
                    label: '不购买'
                }],
                airYna_options: [{
                    value: '存在',
                    label: '购买'
                }, {
                    value: '不存在',
                    label: '不购买'
                }],
                airCna_options: [{
                    value: '存在',
                    label: '购买'
                }, {
                    value: '不存在',
                    label: '不购买'
                }],



                air: {
                    airCode: "",
                    airFna: "",
                    airF: "",
                    airCna: "",
                    airC: "",
                    airYna: "",
                    airY: "",
                    airTotal: ""
                },

                rules: {
                    airF: [
                        {validator: validF, trigger: "blur"}
                    ],
                    airY: [
                        {validator: validY, trigger: "blur"}
                    ],
                    airC: [
                        {validator: validC, trigger: "blur"}
                    ],
                    airCode: [
                        {required: true, message: "请选择机型", trigger: "blur"}
                    ],

                },

            }
        }
    }
</script>

<style scoped>

</style>